package com.huohuo.springdemo.proxy.cglib;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 *增强
 */
public class UserServiceInterceptor implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("*******"+o.getClass().getName());//*******com.sun.proxy.$Proxy70
        User2 user = (User2) objects[0];
        if (user.getName().equals( "王" )){
            throw new Exception( "名字不能姓王" );
        }
        //上面是前置增强
        //调用父类的方法，即调用UserServiceImpl类中的方法
        Object object = methodProxy.invokeSuper(o,objects);
        //后置增强
        System.out.println("业务逻辑增强，invokeSuper()方法执行结束");
        return object;
    }



}
